package com.battle.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.FeedBack;
import com.battle.pojo.entity.Organization;
import com.battle.pojo.entity.User;
import com.battle.pojo.param.feedBack.*;
import com.battle.pojo.vo.feedBack.*;
import com.battle.pojo.dto.feedBack.*;
import com.battle.service.repository.in.FeedBackRepository;
import com.battle.service.repository.in.OrganizationRepository;
import com.battle.service.repository.in.UserRepository;
import com.battle.service.service.in.FeedBackService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 【描 述】：战例反馈服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class FeedBackServiceImpl implements FeedBackService {

    private final FeedBackRepository feedBackRepository;
    private final UserRepository userRepository;
    private final OrganizationRepository organizationRepository;
    public FeedBackServiceImpl(FeedBackRepository feedBackRepository, UserRepository userRepository, OrganizationRepository organizationRepository) {
        this.feedBackRepository = feedBackRepository;
        this.userRepository = userRepository;
        this.organizationRepository = organizationRepository;
    }


    /**
     * 创建战例反馈
     *
     * @param param
     */
    @Override
    public void createFeedBack(CreateFeedBackParam param) {
        FeedBack feedBack = new FeedBack();
        BeanUtils.copyProperties(param, feedBack);
        Long id = IdGenerator.SNOW.generate();
        feedBack.setId(id);
        feedBack.setFeedbackDate(LocalDateTime.now());
        feedBackRepository.create(feedBack);
    }

    /**
     * 修改战例反馈
     *
     * @param param
     */
    @Override
    public void editFeedBack(EditFeedBackParam param) {
        Long id = param.getId();
        FeedBack feedBack = feedBackRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, feedBack);
        feedBackRepository.edit(feedBack);
    }

    /**
     * 移除战例反馈
     *
     * @param id
     */
    @Override
    public void removeFeedBack(Long id) {
        feedBackRepository.removeById(id);
    }

    /**
     * 启动/禁用战例反馈
     *
     * @param id
     */
    @Override
    public void enabledFeedBack(Long id) {
        FeedBack feedBack = feedBackRepository.getByIdCheck(id);
        feedBack.setIsDisable(!feedBack.getIsDisable());
        feedBackRepository.edit(feedBack);
    }

    /**
     * 获取战例反馈详情
     *
     * @param id
     * @return
     */
    @Override
    public GetFeedBackVo getFeedBack(Long id) {
        FeedBack feedBack = feedBackRepository.getByIdCheck(id);
        GetFeedBackVo vo = new GetFeedBackVo();
        BeanUtils.copyProperties(feedBack, vo);
        return vo;
    }

    /**
     * 获取战例反馈 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetFeedBackListVo> getFeedBackList(GetFeedBackListParam param) {

        SelectFeedBackDto dto = new SelectFeedBackDto();
        BeanUtils.copyProperties(param, dto);
        Page<FeedBack> page = feedBackRepository.selectFeedBack(dto);
        List<FeedBack> feedBackList = page.getRecords();
        List<GetFeedBackListVo> list = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(feedBackList)){
            List<Long> userIds = new ArrayList<>();
            List<Long> deptIds = new ArrayList<>();

            feedBackList.forEach(v -> {
                userIds.add(v.getFeedbackUserId());
                deptIds.add(v.getFeedbackDepartmentId());
            });

            Map<Long, User> userMap = userRepository.getMap(userIds);
            Map<Long, Organization> deptMap = organizationRepository.getMap(deptIds);

            for (FeedBack feedBack : feedBackList) {
                GetFeedBackListVo vo = new GetFeedBackListVo();
                BeanUtils.copyProperties(feedBack, vo);
                User operation = userMap.get(vo.getFeedbackUserId());
                if (operation != null) {
                    vo.setFeedbackUserName(operation.getName());
                }
                Organization dept = deptMap.get(vo.getFeedbackDepartmentId());
                if (dept != null) {
                    vo.setFeedbackDepartmentName(dept.getName());
                }
                list.add(vo);
            }
        }

        return new PageResult<>(list, page);

    }

    /**
     * 获取战例反馈下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetFeedBackDownListVo> getFeedBackDownList(GetFeedBackDownListParam param) {
        FeedBack po = new FeedBack();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<FeedBack> feedBackList = feedBackRepository.getListByEntity(po);

        List<GetFeedBackDownListVo> list = new ArrayList<>();
        for (FeedBack feedBack : feedBackList) {
            GetFeedBackDownListVo vo = new GetFeedBackDownListVo();
            BeanUtils.copyProperties(feedBack, vo);
            list.add(vo);
        }
        return list;
    }


}



